package inabit.data.expression;

import inabit.base.WorkingMemory;
import inabit.data.Variable;

import java.util.ArrayList;
import java.util.List;
/**This class represents a boolean expression
 * @author Rafael Rocha*/
public abstract class Expression {
	private boolean not;
	/**returns true if this expression is preceded by a not clause*/
	public boolean hasNot() {
		return not;
	}
	
	/**returns a copy of this expression but negated*/
	public Expression negation() {
		Expression result = copy();
		result.not = !not;
		return result;
	}
	/**Returns a copy of this expression*/
	public Expression clone() {
		Expression copy = copy();
		copy.not = not;
		return copy;
	}
	/**Returns a copy of the expression without consider if it is preceded by the 'not' modifier*/
	protected abstract Expression copy();
	/**returns a truth value of this expression ranging from 0(false) to 1(true)*/
	public abstract Double evaluate(WorkingMemory wm);
	/**Returns the tagged string representation of this expressions without considering
	 * the 'not' modifier*/
	public abstract String stringForm();
	
	/**Returns a tagged string representation of this expression*/
	public String toString() {
		return not?"<not> (" + stringForm() + ")" : stringForm();
	}
	/**Returns all variables referenced by this expression*/
	public abstract List<String> getReferencedVariablesIds();
	/**Get all variables with id returned by getReferencedVariablesIds*/
	public List<Variable> getReferencedVariables(WorkingMemory wm) {
		List<Variable> variables = new ArrayList<Variable>();
		for(String id : getReferencedVariablesIds()) {
			variables.add(wm.getVariable(id));
		}
		return variables;
	}
}
